package projetobancodedados;

import java.sql.*;
import java.util.ArrayList;
/* 
*/
public class professorControle {
    connectionFactory cnFact;
    Connection con;
    public professorControle()
    {
        cnFact = new connectionFactory();
        con = cnFact.getConnection();
    }
    public void addProfessor(professor p)
    {
        try{
            String doc = p.getID();
            String nome = p.getNome();
            String curso = p.getCurso();
            String disc = p.getDisc();
            String pesq = p.getPesquisa();
        
            String querry = "INSERT INTO professor(doc_professor, nome, curso, disciplina, linha_de_pesquisa) VALUES (?, ?, ?, ?, ?)";
            PreparedStatement ps = con.prepareStatement(querry);
            ps.setString(1, doc);
            ps.setString(2, nome);
            ps.setString(3, curso);
            ps.setString(4, disc);
            ps.setString(5, pesq);
            ps.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public ArrayList listaProfessores(){
       ArrayList<professor> professores = new ArrayList();
       professor p;
       try{
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery("SELECT * FROM professor");
            while(rs.next()){
                p = new professor(rs.getString("doc_professor"),rs.getString("nome"),rs.getString("curso"
                        + ""), rs.getString("disciplina"),rs.getString("linha_de_pesquisa"));
                professores.add(p);
            }
       }catch(Exception e){
           e.printStackTrace();
       }
       return professores;
    }
    
    public void closeConnection()
    {
        try{
            con.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
